package com.pro_one.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pro_one.dao.PermissionDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.pojo.Permission;
import com.pro_one.service.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult findAllPermission(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        Page<Permission> permissions=null;
        if (queryString==null||queryString.isEmpty()) {
            permissions = permissionDao.findAllPermission();
        }else {
            permissions=permissionDao.findAllPermissionQueryString(queryString);
        }
        if (permissions==null||permissions.isEmpty()) {
            return new PageResult(null,null);
        }else {
            return new PageResult(permissions.getTotal(),permissions.getResult());
        }



    }

    @Override
    public void addPermission(Permission permission) {
        permissionDao.addPermission(permission);
    }

    @Override
    public void deletePermission(Integer id) {
        permissionDao.deletePermission(id);
    }

    @Override
    public void updatePermission(Permission permission) {
        permissionDao.updatePermission(permission);
    }

    @Override
    public Set findPermissionsByRoleId(Integer id) {
        return null;
    }

    @Override
    public PageResult findAllPermissionByRoleId(Integer page,Integer size,Integer roleId) {
        PageHelper.startPage(page,size);
        Page<Map> allPermissionByRoleId = permissionDao.findAllPermissionByRoleId(roleId);
        return new PageResult(allPermissionByRoleId.getTotal(),allPermissionByRoleId.getResult());
    }

    @Override
    public Integer[] findPermissionIdsByRoleId(Integer roleId) {
        Integer[] permissionIdsByRoleId = permissionDao.findPermissionIdsByRoleId(roleId);
        return permissionIdsByRoleId;
    }
}
